﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Health;
using Microsoft.Health.ItemTypes;
using System.IO;

namespace HealthVaultPowerShell
{
    public class HealthVaultShellUtilities
    {
        private Guid powerShellApplicationId;
        private Uri platformUri;
        private Uri powerShellUri;

        public HealthVaultShellUtilities(Guid masterApplicationId, Uri appPlatformUri, Uri appShellUri)
        {
            powerShellApplicationId = masterApplicationId;
            platformUri = appPlatformUri;
            powerShellUri = appShellUri;
        }

        public string GetClientAppAuthFileNameFullPath()
        {
            string docPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string fileName = "HealthVaultPowerShellClientApplicationId.txt";
            return docPath + @"." + fileName;
        }

        public Guid GetClientId()
        {
            string s;
            using (var r = new StreamReader(new FileStream(GetClientAppAuthFileNameFullPath(), FileMode.Open)))
            {
                s = r.ReadToEnd();
            }
            return new Guid(s);
        }

        public HealthClientApplication GetClient()
        {
            HealthClientApplication clientApp = HealthClientApplication.Create(
                this.GetClientId(), powerShellApplicationId, powerShellUri, platformUri);
            return clientApp;
        }

        public Guid NameToTypeId(string name)
        {
            switch (name)
            {
                case "weight":
                    return Weight.TypeId;
                case "sleep":
                    return SleepJournalAM.TypeId;
                case "bp":
                    return BloodPressure.TypeId;
                case "bg":
                    return BloodGlucose.TypeId;
                case "exercise":
                    return Exercise.TypeId;
                case "basic":
                    return Basic.TypeId;
                case "personImage":
                    return PersonalImage.TypeId;
                case "labResult":
                    return LabTestResults.TypeId;
                case "ccd":
                    return CCD.TypeId;
                case "ccr":
                    return CCR.TypeId;
                default:
                    return BasicV2.TypeId;
            }
        }
    }
}
